package filter;

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.HashMap;

public class Filter {
	private FileOperation file;
	private HashMap<String, Handler> hd;
	
	Filter(){
		hd = new HashMap<String, Handler>();
		setHandler();
	}
	
	public void delFile(String path) {
		file = new FileOperation(path);
		file.delFile();
	}
	
	public void renameFile(String path) {
		file = new FileOperation(path);
		System.out.println("input the new file name : ");
		Scanner in = new Scanner(System.in);
		file.renameFile(in.nextLine());
		in.close();
	}
	
	public void setHandler() {
		hd.put("-chk", new HandlerAttribute(this));
		hd.put("-info", new HandlerGetInfo(this));
		hd.put("-chg", new HandlerChgInfo(this));
		hd.put("-del", new HandlerDel(this));
		hd.put("-rn", new HandlerRename(this));
		hd.put("-zip", new HandlerZip(this));
	}
	
	public void handlerError() {
		System.out.println("Command Not Found!\n"
				+ "You have to check the input\n"
				+ "Or input the \"--help\" for searching for help");
	}
	
	public void compressFile(String inputFile, String outputZip) {
		this.file = new FileOperation(inputFile);
	
		try {
			file.compress(outputZip);
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isHeaderEqualsSuffix(String path) {
		file = new FileOperation(path);
		return file.getFileType().equals(file.getFileSuffix());
	}
	
	public void chgFileProp(String path) {
		for(String info : this.getFileInfo(path)) {
			System.out.println(info);
		}
		if(!this.file.isDirectory()) {
			Scanner in = new Scanner(System.in);
			String answer;
			
			System.out.println("Rename the file? (y / n)");
			while(!((answer = in.nextLine()).equals("y") || answer.equals("n"))) {
				System.out.println("Input (y / n)!");
			}
			if(answer.equals("y")) {
				System.out.println("Input the file name : ");
				file.renameFile(in.nextLine());
			}
			
			System.out.println("Do you want to change ReadOnly jurisdiction? (y / n)");
			while(!((answer = in.nextLine()).equals("y") || answer.equals("n"))) {
				System.out.println("Input (y / n)!");
			}
			if(answer.equals("y")) {
				file.chgRdAuFile();
			}
			
			System.out.println("Do you want to change Hidden jurisdiction?(y / n)");
			while(!((answer = in.nextLine()).equals("y") || answer.equals("n"))) {
				System.out.println("Input (y / n)!");
			}
			if(answer.equals("y")) {
				file.chgHdAuFile();
			}
			in.close();
		}
	}
	
	public String[] getFileInfo(String path) {
		this.file = new FileOperation(path);
		
		if(!file.isDirectory()) {
			return this.file.toString().substring(1, file.toString().length() - 1).split(", ");
		} else {
			return "That`s a Directory".split(" ");
		}
	}
	
	public void doHelp(String cmd) {
		int num = 0, length = 0;
		if(cmd != null) {
			String[] cmds = cmd.split(" ");
			length = cmds.length;
			
			for(String eachCmd : cmds) {
				Handler handle = hd.get(eachCmd);
				if(handle != null) {
					handle.doTips();
				} else {
					num++;
				}
			}
		} 
		if(num == length){
			System.out.println("Usage : filter [options] [Path | --help]\n"
					+ "--help [command1] [command2] ...	Display this help and exit\n"
					+ "-chk [path]	check whether the suffix equals to type(fileHead)\n"
					+ "-chg [path]	change the attribute\n"
					+ "-info [path]	show the file`s information");
		}
	}
	
	public void filt(String cmd, String path) {
		Handler handle = hd.get(cmd);
		
		if(cmd.equals("--help")) {
			this.doHelp(path);
		}
		else if(handle != null ) {
			if(path == "--help") {
				handle.doTips();
			} else {
				try {
					handle.doCmd(path);
					System.out.println("done");
				} catch(Exception e) {
					e.printStackTrace();
					this.handlerError();
				}
			}
		} else {
			this.handlerError();
		}
	}
	
	public static void main(String[] args) {
		Filter filter = new Filter();
		
		if(args.length == 1) {
			filter.filt(args[0], null);
		} else if(args.length == 2) {
			filter.filt(args[0], args[1]);
		} else if(args.length == 3) {
			String sumCmd = "";
			
			for(int i = 1; i < args.length; ++i) {
				sumCmd += (args[i] + " ");
			}
			filter.filt(args[0], sumCmd);
		} else {
			filter.handlerError();
		}
	}
}
